/*
 * berton
 */
package com.pitop.t.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.TroleException;
import com.pitop.t.dao.TroleDAO;
import com.pitop.t.entity.Role;
import com.pitop.t.entity.RoleState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("troleSV")
@Slf4j
public class TroleSVImpl extends BaseSVImpl<Role, Long> implements TroleSV {

    @Autowired
    private TroleDAO troleDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return troleDAO;
    }

    /**
    * 保存account对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(Role entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象Trole
     * @param roleId 
     * @return Role
     */
    @Override
    public Role load(java.lang.Integer roleId) {
           if(roleId==null){
                 throw new TroleException(BaseException.BaseExceptionEnum.Ilegal_Param);
           }

            Map<String,Object> param=new HashMap<>();
            param.put("roleId",roleId);
            return troleDAO.load(param);
    }
    /**
     * 加载一个对象Trole 通过roleId
     * @param roleId 
     * @return Role
     */
    @Override
    public Role loadByRoleId(java.lang.Integer roleId) {
            if(roleId==null){
                 throw new TroleException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return null;
    }


    /**
    * 根据主键roleId,oldStates 共同更新 Role 的状态到newState状态
    *
    * @param roleId 
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateByRoleId(java.lang.Integer roleId, RoleState newState, RoleState... oldStates){
        if(roleId==null){
            throw new TroleException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new TroleException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new TroleException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        troleDAO.updateStateByRoleId(roleId,new Date(),newState,oldStates);
    }

    /**
    * 根据主键roleId 更新 Role 的状态到另一个状态
    *
    * @param roleId 
    * @param state 状态
    */
    @Override
    public void updateByRoleId(java.lang.Integer roleId,RoleState state){
            if(roleId==null){
               throw new TroleException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          troleDAO.updateByRoleId(roleId,state,new Date());
    }


    /**
     * 删除对象Trole
     * @param roleId 
     * @return Role
     */
    @Override
    public void delete(java.lang.Integer roleId) {
            if(roleId==null){
                throw new TroleException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            Map<String,Object> param=new HashMap<>();
            param.put("roleId",roleId);
            troleDAO.delete(param);
    }



    /**
    * 查询Trole分页
    *
    * @param trole  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<Role>
    */
    @Override
    public List<Role> list(Role trole, int offset, int limit) {
            if (offset < 0) {
               offset = 0;
            }

            if (limit < 0) {
               limit = Page.limit;
            }

            Map<String, Object> map = null;
            if (trole != null) {
               map = JSON.parseObject(JSON.toJSONString(trole, SerializerFeature.WriteDateUseDateFormat));
            } else {
               map = new HashMap<>();
            }
        return troleDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(Role trole){
        Map<String, Object> map = null;
        if (trole != null) {
           map = JSON.parseObject(JSON.toJSONString(trole, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return troleDAO.count(map);
     }

    /**
     * 查询Trole分页
     *
     * @param roleId  
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Role>
     */
    @Override
    public List<Role> list(java.lang.Integer roleId, int offset, int limit) {
            if (offset < 0) {
              offset = 0;
            }

            if (limit < 0) {
              limit = Page.limit;
            }
            Map<String,Object> param=new HashMap<>();
            param.put("roleId",roleId);
            return troleDAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Integer roleId) {
            Map<String,Object> param=new HashMap<>();
            param.put("roleId",roleId);
            return troleDAO.count(param);
    }

}
